package servlets;

import java.io.IOException;

import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jpa.entities.Klant;
import jpa.interfaces.KlantBeanInterface;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		forwardTo("WEB-INF/register.jsp", request, response);
	}
	
	private void forwardTo(String jsp, HttpServletRequest request,
			HttpServletResponse response) {
		RequestDispatcher rd = request.getRequestDispatcher(jsp);
		try {
			rd.forward(request, response);
		} catch (Exception e) {

		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try{
			Klant k = new Klant();
			k.setAchternaam(request.getParameter("achternaam"));
			k.setKlant_adres(request.getParameter("straatnaam") +" " +request.getParameter("huisnummer")+" "+request.getParameter("toevoeging"));
			k.setKlant_gebruikersnaam(request.getParameter("gebruikersnaam"));
			k.setKlant_opmerkingen(request.getParameter("opmerking"));
			k.setKlant_postcode(request.getParameter("postcode"));
			k.setKlant_telefoon(request.getParameter("telefoon"));
			k.setKlant_woonplaats(request.getParameter("woonplaats"));
			k.setVoornaam(request.getParameter("voornaam"));
			
			InitialContext	context = new InitialContext();
			KlantBeanInterface kbin = (KlantBeanInterface) context.lookup("KlantBeans");
			if(!kbin.regKlant(k).equals("fout")){
		
				forwardTo("WEB-INF/regged.jsp", request, response);
			}
			else
				forwardTo("WEB-INF/register.jsp", request, response);	
		}
		catch(Exception e)
		{
			
		}
	}

}
